//路由配置js
import { createRouter, createWebHistory } from 'vue-router'; // history
const routes = [
    {
        path: '/',
        component: () => import('@/views/Home/Home.vue'), // 懒加载
        meta: {
            title: '用户中心',
            requireAuth: true
        }
    },
    {
        path: '/login',
        component: () => import('@/views/Login/Login.vue') // 懒加载
    }
]

const router = createRouter({
    routes,
    // 路由模式
    // history: createWebHashHistory()    // hash 模式
    history: createWebHistory()  // history 模式
});
//全局前置路由守卫-- 初始化的时候被调用,每次路由切换之前被调用
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next();
    } else {
        let token = localStorage.getItem('Authorization');
        if (token === null || token === ''||token=='null') {
            next('/login');
        } else {
            next();
        }
    }
});
export default router;